package com.cjjd.c.feature.home.hoteldevice;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.ActionBarFragment;
import com.cjjd.c.base.MyBaseAdapter;
import com.cjjd.c.bean.HotelFacilityInfoBean;
import com.cjjd.c.bean.HotelFacilityShowModel;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.bean.ListResult;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.ImageUtil;
import com.cjjd.c.utils.LogUtil;
import com.cjjd.c.utils.ScreenUtil;
import com.cjjd.c.utils.SharePrefUtil;
import com.cjjd.c.utils.ToastUtil;
import com.cjjd.c.utils.UIUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by dada on 2017/12/23.
 */

public class HomeHotelDeviceFragment extends ActionBarFragment {
    @Bind(R.id.gridView)
    GridView gridView;
    Madapter pictureAdapter;

    @Override
    public Object getTitleString() {
        return "酒店设施";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_bg;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        pictureAdapter = new Madapter(mContext);


        donet();
    }

    private void donet(){
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId));
        h1.put("hotelId", SharePrefUtil.getString(mContext, ShareKeys.HomeHotelUuid));
        service.getHotelFacilityTypes(JsonUtils.convertParam("getHotelFacilityTypes", h1)).enqueue(new CallbackImpl<ListResult<HotelFacilityShowModel>>() {
            @Override
            public void responseSuccess(Call<HttpResult<ListResult<HotelFacilityShowModel>>> call, Response<HttpResult<ListResult<HotelFacilityShowModel>>> response) {

                List<HotelFacilityShowModel> list=response.body().data.dataList;


                int num404=3-list.size()%3;

                for(int i=0;i<num404&&num404!=3;i++){
                    HotelFacilityShowModel bean=new HotelFacilityShowModel();
                    bean.is404=true;
                    list.add(bean);
                }
                pictureAdapter.setData(list);
                gridView.setAdapter(pictureAdapter);
                pictureAdapter.notifyDataSetChanged();

            }
        });

    }




    class Madapter  extends MyBaseAdapter {

        int w;
        public Madapter(FragmentActivity mContext) {
            super(mContext);
            w = ScreenUtil.getScreenWidth(mContext) / 3;

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewholder = null;
            if (convertView == null) {

                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_rooms, parent, false);
                viewholder = new ViewHolder(convertView);
                UIUtil.setRelativeLayoutParams2(convertView, RelativeLayout.LayoutParams.MATCH_PARENT, w*3/4 );

                convertView.setTag(viewholder);
            } else {
                viewholder = (ViewHolder)convertView.getTag();
            }


            final HotelFacilityShowModel bean = (HotelFacilityShowModel) getItem(position);
            if(bean.is404){
                viewholder.img.setBackgroundResource(R.drawable.xydwicon404);
                viewholder.txt.setText(" ");
            }else{
                ImageUtil.displayImage(bean.iconUrl,viewholder.img,R.drawable.icon_jqqd);
                viewholder.txt.setText(bean.typeName);
            }



            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//
                    if(bean.hotelfacilityUuid.equalsIgnoreCase("---")){
                        ToastUtil.showToast("设施暂时无法使用");
                        return;
                    }

                    HashMap<String, Object> h1 = new HashMap<>();
                    h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token));
                    h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId));
//                    h1.put("hotelId", SharePrefUtil.getString(mContext, ShareKeys.HomeHotelUuid));
                    h1.put("hotelFacilityInfoUuid",bean.hotelfacilityUuid);
                    service.getHotelFacilitiesDetails(JsonUtils.convertParam("getHotelFacilitiesDetails", h1)).enqueue(new CallbackImpl<HotelFacilityInfoBean>() {
                        @Override
                        public void responseSuccess(Call<HttpResult<HotelFacilityInfoBean>> call, Response<HttpResult<HotelFacilityInfoBean>> response) {


                            HomeHotelDeviceDetailActivity.lauch(mContext,response.body().data.facilityInfo);

                        }
                    });
                }
            });

            return convertView;
        }

        public class ViewHolder {
            @Bind(R.id.img)
            ImageView img;
            @Bind(R.id.txt)
            TextView txt;
            @Bind(R.id.dian)
            ImageView dian;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }

    }

}
